namespace WpfWatermarkTextBox.Behaviors
{
	#region #using Directives

	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Documents;
	using System.Windows.Media;

	#endregion

	public class TextBlockAdorner : Adorner
	{
		private readonly UIElement adornedElement;
		private readonly TextBlock m_TextBlock;

		public TextBlockAdorner(UIElement adornedElement, string label, Style labelStyle)
			: base(adornedElement)
		{
			this.adornedElement = adornedElement;
			this.m_TextBlock = new TextBlock {
			                                 	Style = labelStyle,
			                                 	Text = label
			                                 };
		}

		protected override int VisualChildrenCount
		{
			get { return 1; }
		}

		protected override Size MeasureOverride(Size constraint)
		{
			this.m_TextBlock.Measure(constraint);
			if (double.IsInfinity(constraint.Height))
			{
				constraint.Height = this.adornedElement.DesiredSize.Height;
			}
			return constraint;
		}

		protected override Size ArrangeOverride(Size finalSize)
		{
			this.m_TextBlock.Arrange(new Rect(finalSize));
			return finalSize;
		}

		protected override Visual GetVisualChild(int index)
		{
			return this.m_TextBlock;
		}
	}
}